package fractal.filter;

public class ContrastFilter extends FractalFilter
{
	int rmeans, gmeans, bmeans;
	double contrast = 1.53;

	@Override
	protected void initFilter()
	{
		int tr = 0, tg = 0, tb = 0;
		double redSum = 0, greenSum = 0, blueSum = 0;
		double total = width * height;
		for (int i = 0; i < total; i++)
		{
			tr = (inPixels[i] >> 16) & 0xff;
			tg = (inPixels[i] >> 8) & 0xff;
			tb = inPixels[i] & 0xff;
			redSum += tr;
			greenSum += tg;
			blueSum += tb;
		}
		rmeans = (int) (redSum / total);
		gmeans = (int) (greenSum / total);
		bmeans = (int) (blueSum / total);
	}
	@Override
	protected int calculatePixel(int i)
	{
		int ta = 0, tr = 0, tg = 0, tb = 0;
		ta = (inPixels[i] >> 24) & 0xff;
		tr = (inPixels[i] >> 16) & 0xff;
		tg = (inPixels[i] >> 8) & 0xff;
		tb = inPixels[i] & 0xff;
		tr -= rmeans;
		tg -= gmeans;
		tb -= bmeans;
		tr = (int) (tr * contrast + rmeans);
		tg = (int) (tg * contrast + gmeans);
		tb = (int) (tb * contrast + bmeans);
		return (ta << 24) | (clamp(tr) << 16) | (clamp(tg) << 8) | clamp(tb);
	}
}
